package com.mdd.admin.service.order.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mdd.admin.service.order.IPreAuthorizationRecordService;
import com.mdd.admin.validate.order.PreAuthorizationRecordCreateValidate;
import com.mdd.admin.validate.order.PreAuthorizationRecordSearchValidate;
import com.mdd.admin.validate.order.PreAuthorizationRecordUpdateValidate;
import com.mdd.admin.vo.order.PreAuthorizationRecordDetailVo;
import com.mdd.admin.vo.order.PreAuthorizationRecordListedVo;
import com.mdd.common.base.page.PageValidate;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.order.PreAuthorizationRecord;
import com.mdd.common.mapper.order.PreAuthorizationRecordMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;

/**
 * 预授权记录实现类
 * @author LikeAdmin
 */
@Service
public class PreAuthorizationRecordServiceImpl implements IPreAuthorizationRecordService {

    @Resource
    PreAuthorizationRecordMapper preAuthorizationRecordMapper;

    /**
     * 预授权记录列表
     *
     * @author LikeAdmin
     * @param pageValidate 分页参数
     * @param searchValidate 搜索参数
     * @return PageResult<OrderPreAuthorizationRecordListedVo>
     */
    @Override
    public PageResult<PreAuthorizationRecordListedVo> list(PageValidate pageValidate, PreAuthorizationRecordSearchValidate searchValidate) {
        Integer page  = pageValidate.getPageNo();
        Integer limit = pageValidate.getPageSize();

        QueryWrapper<PreAuthorizationRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");

        preAuthorizationRecordMapper.setSearch(queryWrapper, searchValidate, new String[]{
            "=:orderId@order_id:long",
            "=:preAuthorizationNo@pre_authorization_no:str",
            "=:preAuthorizationMeny@pre_authorization_meny:str"
        });

        IPage<PreAuthorizationRecord> iPage = preAuthorizationRecordMapper.selectPage(new Page<>(page, limit), queryWrapper);

        List<PreAuthorizationRecordListedVo> list = new LinkedList<>();
        for(PreAuthorizationRecord item : iPage.getRecords()) {
            PreAuthorizationRecordListedVo vo = new PreAuthorizationRecordListedVo();
            BeanUtils.copyProperties(item, vo);
            list.add(vo);
        }

        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
    }

    /**
     * 预授权记录详情
     *
     * @author LikeAdmin
     * @param id 主键参数
     * @return OrderPreAuthorizationRecord
     */
    @Override
    public PreAuthorizationRecordDetailVo detail(Long id) {
        PreAuthorizationRecord model = preAuthorizationRecordMapper.selectById(id);

        Assert.notNull(model, "预授款扣款记录数据不存在");

        PreAuthorizationRecordDetailVo vo = new PreAuthorizationRecordDetailVo();
        BeanUtils.copyProperties(model, vo);
        return vo;
    }

    /**
     * 预授权记录新增
     *
     * @author LikeAdmin
     * @param createValidate 参数
     */
    @Transactional
    @Override
    public void add(PreAuthorizationRecordCreateValidate createValidate) {
        PreAuthorizationRecord model = new PreAuthorizationRecord();
        model.setOrderId(createValidate.getOrderId());
        model.setPreAuthorizationNo(createValidate.getPreAuthorizationNo());
        model.setPreAuthorizationMeny(createValidate.getPreAuthorizationMeny());
        model.setStatus(createValidate.getStatus());
        preAuthorizationRecordMapper.insert(model);
    }

    /**
     * 预授权记录编辑
     *
     * @author LikeAdmin
     * @param updateValidate 参数
     */
    @Transactional
    @Override
    public void edit(PreAuthorizationRecordUpdateValidate updateValidate) {
        PreAuthorizationRecord model = preAuthorizationRecordMapper.selectById(updateValidate.getId());

        Assert.notNull(model, "预授款扣款记录数据不存在!");

        model.setId(updateValidate.getId());
        model.setOrderId(updateValidate.getOrderId());
        model.setPreAuthorizationNo(updateValidate.getPreAuthorizationNo());
        model.setPreAuthorizationMeny(updateValidate.getPreAuthorizationMeny());
        model.setStatus(updateValidate.getStatus());
        preAuthorizationRecordMapper.updateById(model);
    }

    /**
     * 预授权记录删除
     *
     * @author LikeAdmin
     * @param id 主键ID
     */
    @Transactional
    @Override
    public void del(Long id) {
        PreAuthorizationRecord model = preAuthorizationRecordMapper.selectById(id);

        Assert.notNull(model, "预授款扣款记录数据不存在!");

        preAuthorizationRecordMapper.deleteById(id);
    }

}
